package com.at.controller;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Set;

import android.content.Context;
import com.at.appmodel.AlarmManagerModel;
import com.at.appmodel.TimeManagementModel;

public class ATAPPCondition {
	AlarmManagerModel oAlarmManagerModel;
	TimeManagementModel oTimeManagementModel;
	
	public ATAPPCondition(Context context){
		
		oAlarmManagerModel		=	AlarmManagerModel.getInstance(context);
		oTimeManagementModel	= 	TimeManagementModel.getInstance(context);
	}
	
	public boolean meetAlarmConditions(){
		
		if(oTimeManagementModel.isAlarmActive()){
			
			Calendar calfrom, calto;//	=	 Calendar.getInstance();
			Calendar today	= Calendar.getInstance();

			
			calfrom	= oTimeManagementModel.getAlarmtimefrom();
			calto	= oTimeManagementModel.getAlarmtimeto();
			
			Set<String>	weekdays	=	oTimeManagementModel.getSpelectedweekdays();

			if(weekdays.contains(TimeManagementModel.days[today.get(Calendar.DAY_OF_WEEK)-1])){
				if(hmsComparator.compare(today, calfrom) >= 1 &&  hmsComparator.compare(calto, today) >= 1){
					return true;		
				}
			}
		}
		
		return false;
	}
	
    Comparator<Calendar> hmsComparator = new Comparator<Calendar>() {
    	
    	
        @Override
        public int compare(Calendar d1, Calendar d2) {
            return 
                d1.get(Calendar.HOUR_OF_DAY)< d2.get(Calendar.HOUR_OF_DAY) ? -1 :
                d1.get(Calendar.HOUR_OF_DAY) > d2.get(Calendar.HOUR_OF_DAY) ? +1 :
                d1.get(Calendar.MINUTE) < d2.get(Calendar.MINUTE) ? -1 :
                d1.get(Calendar.MINUTE) > d2.get(Calendar.MINUTE) ? +1 :
                d1.get(Calendar.SECOND) < d2.get(Calendar.SECOND) ? -1 :
                d1.get(Calendar.SECOND) > d2.get(Calendar.SECOND) ? +1 :
                0;
        }
    };
}
